package gwprop

import (
	"github.com/magiconair/properties"
	"strings"
)

const (
	//
	GW_EPID_KEY = "gateway.binding.epid"
	GW_GWID_KEY = "gateway.binding.gwid"
	//
	GW_EMQDOMAIN_KEY = "gateway.emq.serverdomain"
	GW_EMQIP_KEY     = "gateway.emq.serverip"
	GW_EMQPORT_KEY   = "gateway.emq.serverport"

	GW_EMQUSER_KEY = "gateway.emq.userid"
	GW_EMQPWD_KEY  = "gateway.emq.userpwd"

	// gateway.device.mac=99:50:AA:AA:03:40
	GW_DEVICE_MAC = "gateway.device.mac"

	// "/root/IIG3000/gwagent/config/gateway.properties"
	GW_Prop_Path = "/app/config/gateway.properties"
	ServerPort   = 8106
)

type GatewayProp struct {
	EPID        string
	GWID        string
	GWEMQDOMAIN string
	EMQIP       string
	EMQPORT     string
	EMQUSER     string
	EMQPWD      string
	Channel     string
}

var prop GatewayProp

func GetProp() GatewayProp {
	return prop
}

func InitProp() {
	p := properties.MustLoadFile(GW_Prop_Path, properties.UTF8)

	m := new(GatewayProp)
	deviceMac := p.MustGet(GW_DEVICE_MAC)
	m.Channel = strings.Replace(deviceMac, ":", "", -1) + ".mq"
	//
	m.EPID = p.MustGet(GW_EPID_KEY)
	m.GWID = p.MustGet(GW_GWID_KEY)
	m.GWEMQDOMAIN = p.MustGet(GW_EMQDOMAIN_KEY)
	m.EMQIP = p.MustGet(GW_EMQIP_KEY)
	m.EMQPORT = p.MustGet(GW_EMQPORT_KEY)
	m.EMQUSER = p.MustGet(GW_EMQUSER_KEY)
	m.EMQPWD = p.MustGet(GW_EMQPWD_KEY)
	prop = *m
}
